package the.one.weather.ui.fragment;

import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.qmuiteam.qmui.util.QMUIKeyboardHelper;

import the.one.base.base.fragment.BaseDataFragment;
import the.one.base.base.presenter.BasePresenter;
import the.one.base.util.EventBusUtil;
import the.one.base.widge.TheSearchView;
import the.one.weather.event.AddEvent;
import the.one.weather.model.City;
import the.one.weather.ui.adapter.LocationAdapter;
import the.one.weather.util.LitePalUtil;


//  ┏┓　　　┏┓
//┏┛┻━━━┛┻┓
//┃　　　　　　　┃
//┃　　　━　　　┃
//┃　┳┛　┗┳　┃
//┃　　　　　　　┃
//┃　　　┻　　　┃
//┃　　　　　　　┃
//┗━┓　　　┏━┛
//    ┃　　　┃                  神兽保佑
//    ┃　　　┃                  永无BUG！
//    ┃　　　┗━━━┓
//    ┃　　　　　　　┣┓
//    ┃　　　　　　　┏┛
//    ┗┓┓┏━┳┓┏┛
//      ┃┫┫　┃┫┫
//      ┗┻┛　┗┻┛

/**
 * @author The one
 * @date 2019/5/9 0009
 * @describe 添加城市
 * @email 625805189@qq.com
 * @remark
 */
public class AddCityFragment extends BaseDataFragment<City> implements TheSearchView.OnTextChangedListener{

    private String mSearch;
    private TheSearchView mSearchView;

    @Override
    protected void initView(View rootView) {
//        mTopLayout.addRightTextButton("", R.id.topbar_right_text);
        addTopBarBackBtn();
        mSearchView = new TheSearchView(_mActivity);
        mSearchView.setOnTextChangedListener(this);
        mTopLayout.setCenterView(mSearchView);
        super.initView(rootView);
        pullLayout.setEnabled(false);
        showEmptyPage("");
    }

    @Override
    protected BaseQuickAdapter getAdapter() {
        return new LocationAdapter();
    }

    @Override
    protected void onLazyInit() {
    }

    @Override
    protected void requestServer() {
        onComplete(LitePalUtil.getSearchCities(mSearch));
        adapter.loadMoreEnd(true);
        setPullLayoutEnabled(false);
    }

    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
        QMUIKeyboardHelper.hideKeyboard(mSearchView.getSearchEditText());
        City city = (City) adapter.getItem(position);
        EventBusUtil.sendEvent(new AddEvent(city.getName()));
        _mActivity.finish();
    }

    @Override
    public boolean onItemLongClick(BaseQuickAdapter adapter, View view, int position) {
        return false;
    }

    @Override
    public void showEmptyPage(String title, String content, String btnString, View.OnClickListener listener) {
        super.showEmptyPage( "", "", "", listener);
    }

    @Override
    public BasePresenter getPresenter() {
        return null;
    }

    @Override
    protected boolean isNeedSpace() {
        return false;
    }

    @Override
    public void onChanged(String content, boolean isEmpty) {
        mSearch = content;
        if (isEmpty) {
            showEmptyPage("");
            return;
        }
        onSearch();
    }

    @Override
    public void onSearch() {
        isFirst = true;
        requestServer();
    }

}
